package org.kymjs.kjframe.auth;

import static org.kymjs.kjframe.common.Constant.KEY_TOKEN;
import static org.kymjs.kjframe.common.Constant.KEY_USER;
import static org.kymjs.kjframe.common.Constant.PREFS_NAME;

import android.content.Context;
import android.content.SharedPreferences;

import com.fasterxml.jackson.databind.ObjectMapper;

public class AuthManage {
    private static volatile AuthManage instance;
    private Authuser user = null;
    private String token = null;

    private final ObjectMapper objectMapper = new ObjectMapper();

    public static AuthManage getInstance() {
        if (instance == null) {
            synchronized (AuthManage.class) {
                if (instance == null) {
                    instance = new AuthManage();
                }
            }
        }
        return instance;
    }

    public void loadUserFromStorage(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        String userJson = sp.getString(KEY_USER, null);
        if (userJson != null) {
            try {
                user = objectMapper.readValue(userJson, Authuser.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public void loadTokenFromStorage(Context context) {
        SharedPreferences sp = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        String token = sp.getString(KEY_TOKEN, null);
        if (token != null) {
            try {
                this.token = token;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public boolean isLoggedIn() {
        return user != null;
    }

    public void logout(Context context) {
        user = null;
        token = "";
        SharedPreferences sp = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
        sp.edit().remove(KEY_USER).apply();
        sp.edit().remove(KEY_TOKEN).apply();
    }

    public void setLoggedInUser(Context context, Authuser user) {
        this.user = user;
        try {
            String userJson = objectMapper.writeValueAsString(user);
            SharedPreferences sp = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            sp.edit().putString(KEY_USER, userJson).apply();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void setLoggedInToken(Context context, String token){
        this.token = token;
        try {
            SharedPreferences sp = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            sp.edit().putString(KEY_TOKEN, token).apply();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public Authuser getUser(Context context) {
        if (user == null){
            loadUserFromStorage(context);
        }
        return user;
    }

    public String getToken(Context context){
        if (token == null){
            loadTokenFromStorage(context);
        }
        return token;
    }
}
